/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modulos.consulta.vista;

import basedatos.ConsultaVisualizarBD;
import basedatos.OperacionesBD;
import java.util.ArrayList;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import modulos.usuario.modelo.Usuario;
import modulos.consulta.modelo.Consulta;
import modulos.consulta.modelo.ConsultaVisualizar;
import vista.frmPrincipal;
import vista.modelo.OperacionesVarias;

/**
 *
 * @author Edulima
 */
public class IFrmCompartirConsultas extends javax.swing.JInternalFrame {

    private ArrayList<Usuario> listaMedicos = new ArrayList<Usuario>();
    private Usuario usuarioSelect = new Usuario();
    private Usuario usuario = new Usuario();
    private Consulta consulta = new Consulta();
    private DefaultTableModel modelotblConsultas = new DefaultTableModel();
    private DefaultTableModel modelotblConsultasAgregadas = new DefaultTableModel();
    private ConsultaVisualizar consultaAgregada = new ConsultaVisualizar();

    /**
     * Creates new form IFrmCompartirConsultas
     */
    public IFrmCompartirConsultas(Consulta consulta) {
        modelotblConsultas.addColumn("Nro");
        modelotblConsultas.addColumn("Motivo Consulta");
        modelotblConsultas.addColumn("Fecha");
        modelotblConsultasAgregadas.addColumn("Nro");
        modelotblConsultasAgregadas.addColumn("Motivo Consulta");
        modelotblConsultasAgregadas.addColumn("Fecha");
        initComponents();
        usuario = (Usuario) frmPrincipal.UsuarioLogueado.get(0)[1];
        this.consulta = consulta;
        listarMedicos();
        cargarConsultas();      
        this.setVisible(true);
    }

    private void cargarConsultas() {
        int c = modelotblConsultas.getRowCount();
        try {
            for (int i = 0; i < c; i++) {
                modelotblConsultas.removeRow(0);
            }
        } catch (Exception ex) {
            ex.getMessage();
        }
        c = modelotblConsultasAgregadas.getRowCount();

        try {
            for (int i = 0; i < c; i++) {
                modelotblConsultasAgregadas.removeRow(0);
            }

        } catch (Exception ex) {
            ex.getMessage();
        }
        consultaAgregada = ConsultaVisualizarBD.buscar(consulta, usuarioSelect);
        if (consultaAgregada == null) {
            Object[] aux = {consulta.getNumConsul(),
                consulta.getMotivo_Consulta(),
                OperacionesVarias.fechaString(consulta.getFechaCreacion())
            };
            modelotblConsultas.addRow(aux);
        } else //consultas visualizar
        {
            Object[] aux = {consultaAgregada.getConsulta().getNumConsul(),
                consultaAgregada.getConsulta().getMotivo_Consulta(),
                OperacionesVarias.fechaString(consultaAgregada.getConsulta().getFechaCreacion())
            };
            modelotblConsultasAgregadas.addRow(aux);
        }
    }

    private void listarMedicos() {
        ArrayList<Usuario> aux = (ArrayList<Usuario>) OperacionesBD.listar("Usuario");
        for (Usuario medico : aux) {
            if (medico.getCuentaUsr().getTipo().equalsIgnoreCase("MEDICO")) {
                if (medico.getIdPersona() != usuario.getIdPersona()) {
                    listaMedicos.add(medico);
                }
            }
        }
        for (Usuario medico : listaMedicos) {
            cmbMedico.addItem(medico);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        tblConsultas = new JTable() {
            public boolean isCellEditable(int rowIndex, int vColIndex) {
                return false;
            }
        };
        jLabel48 = new javax.swing.JLabel();
        jLabel49 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        tblConsultasAgregadas = new JTable() {
            public boolean isCellEditable(int rowIndex, int vColIndex) {
                return false;
            }
        };
        btnAgregar = new javax.swing.JButton();
        btnQuitar = new javax.swing.JButton();
        cmbMedico = new javax.swing.JComboBox();
        jLabel50 = new javax.swing.JLabel();
        btnCerrar = new javax.swing.JButton();

        tblConsultas.setBackground(new java.awt.Color(209, 230, 239));
        tblConsultas.setModel(modelotblConsultas);
        tblConsultas.setToolTipText("Doble Click para Visualizar Consulta");
        tblConsultas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblConsultasMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tblConsultas);

        jLabel48.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N
        jLabel48.setForeground(new java.awt.Color(0, 12, 34));
        jLabel48.setText("CONSULTAS");

        jLabel49.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N
        jLabel49.setForeground(new java.awt.Color(0, 12, 34));
        jLabel49.setText("CONSULTAS AGREGADAS");

        tblConsultasAgregadas.setBackground(new java.awt.Color(209, 230, 239));
        tblConsultasAgregadas.setModel(modelotblConsultasAgregadas);
        tblConsultasAgregadas.setToolTipText("Doble Click para Visualizar Consulta");
        tblConsultasAgregadas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblConsultasAgregadasMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(tblConsultasAgregadas);

        btnAgregar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/accept.png"))); // NOI18N
        btnAgregar.setText("Agregar");
        btnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarActionPerformed(evt);
            }
        });

        btnQuitar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/delete.png"))); // NOI18N
        btnQuitar.setText("Quitar");
        btnQuitar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnQuitarActionPerformed(evt);
            }
        });

        cmbMedico.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbMedicoActionPerformed(evt);
            }
        });

        jLabel50.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N
        jLabel50.setForeground(new java.awt.Color(0, 12, 34));
        jLabel50.setText("ESPECIALISTA");

        btnCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/agt_home.png"))); // NOI18N
        btnCerrar.setText("Cerrar");
        btnCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCerrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(btnAgregar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(btnQuitar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(163, 163, 163)
                                .addComponent(jLabel48)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel49)
                                .addGap(137, 137, 137))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addComponent(jLabel50)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmbMedico, javax.swing.GroupLayout.PREFERRED_SIZE, 455, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbMedico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel50))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel49)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel48)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnAgregar)
                        .addGap(18, 18, 18)
                        .addComponent(btnQuitar)
                        .addGap(82, 82, 82)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                .addComponent(btnCerrar)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tblConsultasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblConsultasMouseClicked
//        if (tblConsultas.getRowCount() > 0) {
//            int aux = tblConsultas.getSelectedRow();
//            if (aux != -1) {
//                consulta = (Consulta) listConsultas.get(aux);
//                btnExamenes.setEnabled(true);
//                if (evt.getClickCount() == 2) {
//
//                    setConsulta(consulta);
//                    agregarPaneles();
//                    btnCerrarConsulta.setVisible(true);
//
//                } else if (evt.getClickCount() == 1) {
//                    cargarEvoluciones();
//                }
//
//            } else {
//                JOptionPane.showMessageDialog(this, "Seleccione la consulta ...", "Visualizar Consulta", JOptionPane.WARNING_MESSAGE);
//            }
//        } else {
//            JOptionPane.showMessageDialog(this, "No ha registrado ninguna consulta ...", "Visualizar Consulta", JOptionPane.WARNING_MESSAGE);
//        }
//
    }//GEN-LAST:event_tblConsultasMouseClicked

    private void tblConsultasAgregadasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblConsultasAgregadasMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_tblConsultasAgregadasMouseClicked

    private void cmbMedicoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbMedicoActionPerformed
        int index = cmbMedico.getSelectedIndex();
        if (index != -1) {
            usuarioSelect = listaMedicos.get(index);
            cargarConsultas();
        }
    }//GEN-LAST:event_cmbMedicoActionPerformed

    private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarActionPerformed
        int index = tblConsultas.getSelectedRow();
        if (index != -1) {
            ConsultaVisualizar cv = new ConsultaVisualizar();
            cv.setConsulta(consulta);
            cv.setUsuario(usuarioSelect);
            OperacionesBD.guardar(cv);
            cargarConsultas();
        }
    }//GEN-LAST:event_btnAgregarActionPerformed

    private void btnQuitarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnQuitarActionPerformed
        int index = tblConsultasAgregadas.getSelectedRow();
        if (index != -1) {
            ConsultaVisualizar cv = consultaAgregada;
            if (OperacionesBD.eliminar(cv, cv.getIdConsultaVisualizar())) {
                consultaAgregada = null;
                cargarConsultas();
            }
        }
    }//GEN-LAST:event_btnQuitarActionPerformed

    private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCerrarActionPerformed
        dispose();
    }//GEN-LAST:event_btnCerrarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregar;
    private javax.swing.JButton btnCerrar;
    private javax.swing.JButton btnQuitar;
    private javax.swing.JComboBox cmbMedico;
    private javax.swing.JLabel jLabel48;
    private javax.swing.JLabel jLabel49;
    private javax.swing.JLabel jLabel50;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable tblConsultas;
    private javax.swing.JTable tblConsultasAgregadas;
    // End of variables declaration//GEN-END:variables
}
